package org.jtb.mensawotd;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.util.Log;

class Fetcher {
	private String content = null;

	Fetcher(String url) throws IOException {
		BufferedReader reader = null;

		try {
		URL u = new URL(url);
		HttpURLConnection uc = (HttpURLConnection) u.openConnection();
		uc.setReadTimeout(30 * 1000); // 30 seconds
		uc.setConnectTimeout(30 * 1000); // 30 seconds
		
		if (uc.getResponseCode() != 200) {
			Log.e("mensawotd",
					"URL read failed, response code: " + uc.getResponseCode());
			return;
		}

		reader = new BufferedReader(new InputStreamReader(uc.getInputStream(),
				"ISO-8859-1"), 8192);

		String line;
		StringBuilder sb = new StringBuilder();
		while ((line = reader.readLine()) != null) {
			sb.append(line);
			sb.append('\n');
		}
		content = sb.toString();
		} finally {
			if (reader != null) {
				reader.close();
			}
		}
	}

	String getContent() {
		return content;
	}
}
